package com.ch.dream.logic.longstory;

import android.app.ProgressDialog;
import android.widget.EditText;
import android.widget.TextView;
import cn.goso.utility.NumberTools;

import com.ch.dream.R;
import com.ch.dream.activity.longstory.AddDreamActivity;
import com.ch.dream.asynctask.AddDreamRequestAsyncTask;
import com.ch.dream.base.BaseLogic;
import com.ch.dream.base.BaseRequestAsyncTask.RequestServerListener;
import com.ch.dream.bean.DreamstoryBean;
import com.ch.dream.common.CommonUserInfo;
import com.ch.dream.tools.UtilTools;
import com.ch.dream.tools.ViewTools;

public class AddDreamLogic extends BaseLogic {
	
	private ProgressDialog loadingDialog;
	private EditText etContent;
	private TextView tvSummary;
	private String longStoryId;
	private String lastContent;
	public AddDreamLogic(AddDreamActivity activity) {
		super(activity);
	}

	public AddDreamLogic(AddDreamActivity activity, int layoutId) {
		super(activity, layoutId);
	}
	
	public void initViews(){
		etContent =(EditText) activity.findViewById(R.id.etContent);
		tvSummary =(TextView) activity.findViewById(R.id.tvSummary);
	}
	
	public void initData(){
		lastContent = activity.getIntent().getStringExtra("content");
		longStoryId = activity.getIntent().getStringExtra("longStoryId");
	}
	
	public void init(){
		initViews();
		initData();
		tvSummary.setText(lastContent);
	}
	
	public void submit(){
		if(validate()){
			String content = etContent.getText().toString();
			int userId = NumberTools.stringToInt(CommonUserInfo.userId);
			DreamstoryBean bean =new DreamstoryBean();
			bean.setContent(content);
			bean.setLongStoryId(NumberTools.stringToInt(longStoryId));
			bean.setCreator(userId);
			new AddDreamRequestAsyncTask(activity, new RequestServerListener() {
				@Override
				public void onRequestServerBegin() {
					loadingDialog = ViewTools.showLoading(activity,"提示", "提交数据中，请稍候...");
				}
				
				@Override
				public void onRequestServerEnd(Object result) {
					try{
						loadingDialog.dismiss();
						if(result!=null){
							ViewTools.showLongToast(activity,"续梦成功，请等待拥有者选择");
							activity.finish();
						}else{
							ViewTools.showShortToast(activity,"网络错误");
						}
					}catch (Exception e) {
						ViewTools.showShortToast(activity,"网络错误");
					}
				}
			}).execute(bean);
		}
	}
	
	public boolean validate(){
		String content = etContent.getText().toString();
		if(!UtilTools.isNotNullOrEmpty(content)){
			ViewTools.showShortToast(activity,"内容不能为空");
			return false;
		}
		return true;
	}

}
